package com.thinkmore.business.service;

import com.thinkmore.business.bean.Role;
import com.thinkmore.business.bean.User;
import com.thinkmore.business.dao.DataBaseDao;
import com.thinkmore.business.dao.RoleMapper;
import com.thinkmore.business.dao.UserDao;
import com.thinkmore.framework.common.RandomUtil;
import com.thinkmore.framework.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static java.util.Arrays.asList;

@Service
public class DataBaseService {

    @Resource
    DataBaseDao dataBaseDao;
    @Resource
    UserDao userDao;
    @Resource
    RoleMapper roleMapper;

    public void createTable(String tableName) {
        switch (tableName) {
            case "user":
                dataBaseDao.createUserTable();
                break;
            case "role":
                dataBaseDao.createRoleTable();
                break;
            default:
        }
    }

    List<Role> roles = asList(
            new Role(1, "superAdmin", "超级管理员"),
            new Role(2, "admin", "普通管理员"),
            new Role(3, "siteAdmin", "站点管理员"),
            new Role(4, "user", "普通用户"));
    Role role = roles.get(3);

    public void addData(String tableName, Integer count) {
        switch (tableName) {
            case "role":
                roleMapper.batchInsert(roles);
                break;
            case "user":
                for (int i = 0; i < count; i++) {
                    User user = null;
                    if (i < 4) {
                        user = new User(i + "", roles.get(i), "tudou" + i, "tudou" + i, "nick_tudou" + i,
                                RandomUtil.getTel(), RandomUtil.getEmail(8, 20), LocalDateTime.now());
                    } else {
                        String mobile = RandomUtil.getTel();
                        user = new User(UUIDUtil.uuid(), role, mobile, RandomUtil.getNum(100000, 999999) + "",
                                RandomUtil.getChineseName(), mobile, RandomUtil.getEmail(8, 20), LocalDateTime.now());
                    }
                    userDao.insert(user);
                }
                break;
            default:
        }
    }
}
